#!/user/bin/env python
# -*- coding: utf-8 -*-
'''
Author: Sjy
LastEditTime:
Description: ribbon 菜单
"""


"""常用函数：
# 设置提示文本在ribbonbar 中间部分
ribbonbar.setModuleText(text: str) -> None

# 设置首位置图标
ribbonbar.tabBar().setTabIcon(0,  QtGui.QIcon('./icons/toolbar/file.png'))
# 获取当前的 category
ribbonbar.currentCategory()

# 获取所有tab的标题
titles = ribbonbar.tabBar().tabTitles()
startIndex = titles.index('开始')
'''
__all__ = []

import os
from PyQt5 import QtWidgets, QtCore, QtGui

import gui.controls.globalvar as GL
import gui.controls.signals as SG
import gui.controls.ribbonbars as ribbonbars


class DESK_toolbar(object):
    def __init__(self, *args, **kwargs):
        # 初始化 注册信号 因为这个是第一个初始化的
        SG.signals.sigStartRunToolbar.connect(lambda *args, **kwargs: ribbonbars.init())

    def setupUi(self, desktop, ribbonbar):
        # 缓存 desktop 和 ribbonbar
        GL.set_value("desktop", desktop)
        GL.set_value("ribbonbar", ribbonbar)

        ribbonbar.tabBar().currentChanged.connect(lambda *args, ribbonbar=ribbonbar: self.changed_ribbonbar_category(ribbonbar))  # type: ignore

    def changed_ribbonbar_category(self, ribbonbar):
        SG.signals.sigChangedRibbonbarCategory.emit(
            {
                "category": ribbonbar.currentCategory(),
                "currentIndex": ribbonbar.tabBar().currentIndex(),
                "currentTitle": ribbonbar.tabBar().tabTitles()[
                    ribbonbar.tabBar().currentIndex()
                ],
                "ribbonbar": ribbonbar,
                "setCategory": lambda category, ribbonbar=ribbonbar: ribbonbar.setCurrentCategory(
                    category
                ),
            }
        )


def demo(self):
    ribbon = GL.get_value("ribbonbar")
    category1 = ribbon.addCategory("Category 1")
    panel = category1.addPanel("Panel 1", showPanelOptionButton=False)
    panel.addSmallButton("Button 1", icon=QtGui.QIcon("./icons/close.png"))
    panel.addSmallButton("Button 2", icon=QtGui.QIcon("./icons/close.png"))
    panel.addSmallButton("Button 3", icon=QtGui.QIcon("./icons/close.png"))
    showCategoryButton2 = panel.addMediumToggleButton(
        "Show/Hide Category 2", icon=QtGui.QIcon("./icons/close.png")
    )
    panel.addVerticalSeparator()
    showCategoryButton3 = panel.addMediumToggleButton(
        "Show/Hide Category 3", icon=QtGui.QIcon("./icons/close.png")
    )
    showCategoryButton45 = panel.addMediumToggleButton(
        "Show/Hide Category 4/5",
        icon=QtGui.QIcon("./icons/close.png"),
        colSpan=2,
        alignment=QtCore.Qt.AlignmentFlag.AlignLeft,
    )
    panel.addLargeButton("Button 6", icon=QtGui.QIcon("./icons/close.png"))
    panel.addVerticalSeparator()
    panel.addMediumButton("Button 7", icon=QtGui.QIcon("./icons/close.png"))
    panel.addMediumButton("Button 8", icon=QtGui.QIcon("./icons/close.png"))

    saveButton = panel.addLargeButton("Button 8", icon=QtGui.QIcon("./icons/close.png"))
    menu = QtWidgets.QMenu()
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 1")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 2")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 3")
    saveButton.setMenu(menu)
    saveButton.setPopupMode(QtWidgets.QToolButton.ToolButtonPopupMode.InstantPopup)

    saveButton = panel.addLargeButton("Button 9", icon=QtGui.QIcon("./icons/close.png"))
    menu = QtWidgets.QMenu()
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 1")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 2")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 3")
    saveButton.setMenu(menu)
    saveButton.setPopupMode(QtWidgets.QToolButton.ToolButtonPopupMode.MenuButtonPopup)

    saveButton = panel.addLargeButton(
        "Button 10", icon=QtGui.QIcon("./icons/close.png")
    )
    menu = QtWidgets.QMenu()
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 1")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 2")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 3")
    saveButton.setMenu(menu)
    saveButton.setPopupMode(QtWidgets.QToolButton.ToolButtonPopupMode.DelayedPopup)

    saveButton = panel.addLargeButton(
        "Button 11", icon=QtGui.QIcon("./icons/close.png")
    )
    menu = saveButton.addRibbonMenu()
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 1")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 2")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 3")
    submenu = menu.addMenu(QtGui.QIcon("./icons/close.png"), "Submenu")
    submenu.addAction(QtGui.QIcon("./icons/close.png"), "Action 4")
    submenu.addAction(QtGui.QIcon("./icons/close.png"), "Action 5")
    submenu.addAction(QtGui.QIcon("./icons/close.png"), "Action 6")
    menu.addSpacing()
    menu.addLabel("This is a custom widget")
    formLayout = menu.addFormLayoutWidget()
    formLayout.addRow(QtWidgets.QLabel("Row 1"), QtWidgets.QLineEdit())
    saveButton.setPopupMode(QtWidgets.QToolButton.ToolButtonPopupMode.InstantPopup)
    panel.addWidget(saveButton)  # noqa

    panel = category1.addPanel("Panel 2")
    button = panel.addMediumButton("Button 8", icon=QtGui.QIcon("./icons/close.png"))
    menu = QtWidgets.QMenu()
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 1")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 2")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 3")
    button.setMenu(menu)
    panel.addMediumButton("Button 9", icon=QtGui.QIcon("./icons/close.png"))
    panel.addSmallButton(
        "This is a very very very very very long button",
        icon=QtGui.QIcon("./icons/close.png"),
        colSpan=3,
    )
    button = panel.addSmallToggleButton(
        "Button 10", icon=QtGui.QIcon("./icons/close.png")
    )
    menu = QtWidgets.QMenu()
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 1")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 2")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 3")
    button.setMenu(menu)
    panel.addSmallToggleButton("Button 11", icon=QtGui.QIcon("./icons/close.png"))
    panel.addSmallToggleButton("Button 12", icon=QtGui.QIcon("./icons/close.png"))
    panel.addSmallButton(
        "This is a very very very very very long button",
        icon=QtGui.QIcon("./icons/close.png"),
        colSpan=3,
    )

    category2 = ribbon.addContextCategory("Context 2")
    panel = category2.addPanel("Panel 1")
    panel.addSmallButton("Button 1", icon=QtGui.QIcon("./icons/close.png"))
    panel.addSmallButton("Button 2", icon=QtGui.QIcon("./icons/close.png"))
    panel.addSmallButton("Button 3", icon=QtGui.QIcon("./icons/close.png"))
    panel.addMediumButton("Button 4", icon=QtGui.QIcon("./icons/close.png"))
    panel.addMediumButton("Button 5", icon=QtGui.QIcon("./icons/close.png"))
    panel.addLargeButton("Button 6", icon=QtGui.QIcon("./icons/close.png"))
    panel.addVerticalSeparator()
    panel.addMediumButton("Button 7", icon=QtGui.QIcon("./icons/close.png"))
    panel.addMediumButton("Button 8", icon=QtGui.QIcon("./icons/close.png"))

    saveButton = panel.addLargeButton("Button 8", icon=QtGui.QIcon("./icons/close.png"))
    menu = QtWidgets.QMenu()
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 1")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 2")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 3")
    saveButton.setMenu(menu)
    saveButton.setPopupMode(QtWidgets.QToolButton.ToolButtonPopupMode.InstantPopup)
    panel.addWidget(saveButton)  # noqa

    saveButton = panel.addLargeButton("Button 9", icon=QtGui.QIcon("./icons/close.png"))
    menu = QtWidgets.QMenu()
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 1")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 2")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 3")
    saveButton.setMenu(menu)
    saveButton.setPopupMode(QtWidgets.QToolButton.ToolButtonPopupMode.MenuButtonPopup)
    panel.addWidget(saveButton)  # noqa

    saveButton = panel.addLargeButton(
        "Button 10", icon=QtGui.QIcon("./icons/close.png")
    )
    menu = QtWidgets.QMenu()
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 1")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 2")
    menu.addAction(QtGui.QIcon("./icons/close.png"), "Action 3")
    saveButton.setMenu(menu)
    saveButton.setPopupMode(QtWidgets.QToolButton.ToolButtonPopupMode.DelayedPopup)
    panel.addWidget(saveButton)  # noqa

    panel.addCalendarWidget()

    category3 = ribbon.addContextCategory("Context 3")
    panel = category3.addPanel("Panel 1")
    panel.addLargeButton("Button 1", icon=QtGui.QIcon("./icons/close.png"))
    panel.addLargeButton("Button 2", icon=QtGui.QIcon("./icons/close.png"))
    panel.addLargeButton("Button 3", icon=QtGui.QIcon("./icons/close.png"))

    showCategoryButton2.clicked.connect(category2.setCategoryVisible)
    showCategoryButton3.clicked.connect(
        lambda checked: category3.setCategoryVisible(not category3.categoryVisible())
    )

    gallery = panel.addGallery(popupHideOnClick=True)
    for i in range(100):
        gallery.addToggleButton(f"item {i+1}", QtGui.QIcon("./icons/close.png"))
    popupMenu = gallery.popupMenu()
    submenu = popupMenu.addMenu(QtGui.QIcon("./icons/close.png"), "Submenu")
    submenu.addAction(QtGui.QIcon("./icons/close.png"), "Action 4")
    popupMenu.addAction(QtGui.QIcon("./icons/close.png"), "Action 1")
    popupMenu.addAction(QtGui.QIcon("./icons/close.png"), "Action 2")
    popupMenu.addSeparator()
    popupMenu.addWidget(QtWidgets.QLabel("This is a custom widget"))  # noqa
    formLayout = popupMenu.addFormLayoutWidget()
    formLayout.addRow(QtWidgets.QLabel("Row 1"), QtWidgets.QLineEdit())

    categories = ribbon.addContextCategories("name", ["Context 4", "Context 5"])
    showCategoryButton45.clicked.connect(
        lambda v: categories.setCategoriesVisible(v)
    )  # PySide2 Bug: use lambda

    panel1 = categories["Context 4"].addPanel("Context 4 Panel 1")
    panel1.addLargeButton("Button 1", icon=QtGui.QIcon("./icons/close.png"))
    panel1.addLargeButton("Button 2", icon=QtGui.QIcon("./icons/close.png"))
    panel1.addLargeButton("Button 3", icon=QtGui.QIcon("./icons/close.png"))

    panel2 = categories["Context 5"].addPanel("Context 5 Panel 1")
    panel2.addLargeButton("Button 4", icon=QtGui.QIcon("./icons/close.png"))
    panel2.addLargeButton("Button 5", icon=QtGui.QIcon("./icons/close.png"))
    panel2.addLargeButton("Button 6", icon=QtGui.QIcon("./icons/close.png"))

    categories1 = ribbon.addCategoriesBy(
        {
            "Category 6": {
                "panels": {
                    "Panel 1": {
                        "showPanelOptionButton": True,
                        "widgets": {
                            "Button 1": {
                                "type": "Button",
                                "arguments": {
                                    "icon": QtGui.QIcon("./icons/close.png"),
                                    "text": "Button",
                                    "tooltip": "This is a tooltip",
                                },
                            },
                            "Button 2": {
                                "type": "Button",
                                "arguments": {
                                    "icon": QtGui.QIcon("./icons/close.png"),
                                    "text": "Button 2",
                                    "tooltip": "This is a tooltip",
                                },
                            },
                        },
                    },
                    "Panel 2": {
                        "showPanelOptionButton": True,
                        "widgets": {
                            "Button 3": {
                                "type": "Button",
                                "arguments": {
                                    "icon": QtGui.QIcon("./icons/close.png"),
                                    "text": "Button 3",
                                    "tooltip": "This is a tooltip",
                                },
                            },
                            "Button 4": {
                                "type": "Button",
                                "arguments": {
                                    "icon": QtGui.QIcon("./icons/close.png"),
                                    "text": "Button 4",
                                    "tooltip": "This is a tooltip",
                                },
                            },
                        },
                    },
                },
            }
        }
    )
